<?php
class Mailbox_model extends CI_Model
{
    private $table = 'mailbox';
    
    function findById($id)
    {
        $this->db->where('id', $id);
        return $this->db->get($this->table);
    }
    
    public function total($params = array())
    {
        if (!empty($params['type'])) {
            $this->db->where('type', $params['type']);
        }
        if (!empty($params['title'])) {
            $this->db->like('title', $params['title']);
        }
        return  $this->db->count_all_results($this->table);
    }
    
    public function page_list($page_num, $num, $params = array())
    {
        if (!empty($params['type'])) {
            $this->db->where('type', $params['type']);
        }
        if (!empty($params['title'])) {
            $this->db->like('title', $params['title']);
        }
        $this->db->order_by('id', 'DESC');
        $this->db->limit($page_num, $num);
        return $this->db->get($this->table);
    }
    
    public function insertMailbox($postData)
    {
        $data = array(
            'type'    => trim($postData['type']),
            'title'   => trim($postData['title']),
            'fid'     => trim($postData['fid']), //发布者编号
            'sid'     => array_sum($postData['user_type']), //接收者用户类型或编号
            'fname'   => trim($postData['fname']), //发布者名称
            'sname'   => trim($postData['sname']), //接收者名称 
            'content' => trim($postData['content']),
            'ftime'   => date('Y-m-d H:i:s'),
        );
        $this->db->insert($this->table, $data);
        return $this->db->insert_id();
    }
    
    public function updateMailbox($postData)
    {
        $data = array(
            'type'    => trim($postData['type']),
            'title'   => trim($postData['title']),
            'fid'     => trim($postData['fid']), //发布者编号
            'sid'     => array_sum($postData['user_type']), //接收者用户类型或编号
            'fname'   => trim($postData['fname']), //发布者名称
            'sname'   => trim($postData['sname']), //接收者名称 
            'content' => trim($postData['content']),
            'ftime'   => date('Y-m-d H:i:s'),
        );
        $this->db->where('id', $postData['id']);
        return $this->db->update($this->table, $data);
    }
    
    public function deleteById($id)
    {
        $this->db->where('id', $id);
        return $this->db->delete($this->table);
    }
}